package com.menghao.sso.client.validation;

import com.menghao.sso.client.model.ValidateBean;
import lombok.Setter;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;


/**
 * <p>抽象校验类.<br>
 *
 * @author menghao.
 * @version 2017/11/16.
 */
public abstract class AbstractTicketValidator implements TicketValidator {

    @Setter
    protected RestTemplate restTemplate;
    @Setter
    protected String casServerUrl;

    /**
     * 模版模式
     *
     * @param validateBean 验证包装类
     * @return Boolean 是否验证通过
     * @throws ValidationException
     */
    @Override
    public Boolean validate(ValidateBean validateBean) throws ValidationException {
        try {
            ResponseEntity<Boolean> responseEntity =
                    restTemplate.getForEntity(validateBean.getUrl(), Boolean.class, validateBean.getServiceTicket());
            return parseResponse(responseEntity);
        } catch (RestClientException e) {
            throw new ValidationException(e);
        }
    }

    protected abstract Boolean parseResponse(ResponseEntity<Boolean> responseEntity);
}
